<?php
namespace Music\ApiBundle\Lib;

use Symfony\Component\DependencyInjection\Container;
use Music\ApiBundle\Lib\AngryCurl;

/**
 * NambaGraber
 * Class for parsing www.namba.kg site
 * Used for working with music section of the site
 * 
 * @package Libraries
 * @author Dmitry Levin <dimonmontana@gmail.com>
 */
class NambaGraber {

    /**
     * Self instance
     * @var \Container
     */
    protected $container;

    /**
     * Base site url
     * @var string
     */
    protected $host;

    /**
     * Api url
     * @var string
     */
    protected $api_host;

    /**
     * Base search url
     * @var string 
     */
    protected $search_url;

    /**
     * AngryCurl
     * @var \AngryCurl 
     */
    protected $curl;

    /**
     * Last success request details
     * @var \RollingCurlRequest
     */
    protected $request;

    /**
     * Response from last request
     * @var string
     */
    protected $response;

    /**
     * Information about last request
     * @var array 
     */
    protected $info;

    /**
     * Protected Construct function
     */
    public function __construct(Container $container) {
        $this->container = $container;
        $parameters = $this->container->getParameter('namba');

        $this->host         = $parameters['host'];
        $this->api_host     = $parameters['api_host'];
        $this->search_url   = $parameters['search_url'];
        $this->download_url = $parameters['download_url'];

        $this->curl = new AngryCurl(array($this, 'requestCallback')); 
    }

    /**
     * Angry Curl request callback function
     * 
     * @param string $response
     * @param array $info
     * @param RollingCurlRequest $request
     * @return boolean
     */
    public function requestCallback($response, $info, $request){
       $this->response = $response;
       $this->request  = $request;
       $this->info     = $info;

       return true;
    }

    /**
     * Return last request 
     * @return RollingCurlRequest
     */
    public function getRequest(){
        return $this->request;
    }

    /**
     * Return response
     * @return string
     */
    public function getResponse($type = null){
        if($type == 'json'){
            return json_decode($this->response);
        }
        return $this->response;
    }

    /**
     * Return array info
     * @return array
     */
    public function getInfo(){
        return $this->info;
    }    

    /**
     * Search song by keyword
     * 
     * @param string $keyword
     * @return \Music\ApiBundle\Lib\NambaGraber
     */
    public function searchSong($keyword){
        $request_url = $this->api_host.$this->search_url.$keyword;
        $this->curl->get($request_url);
        $this->curl->execute();

        return $this;
    }

    /**
     * Return download page 
     * @param string $id
     * @return \Music\ApiBundle\Lib\NambaGraber
     */
    public function loadStreamFile($id){
        $request_url = $this->download_url.$id;
        $this->curl->get($request_url);
        $this->curl->execute();

        return $this;
    }
}
